<?php
class questController extends AController {
	private $tableName = 'QUESTION';
	
	public function indexAction() {
		$page = $this->request->getGetParameter('p',1);
		$pagesize = $this->request->getGetParameter('ps',10);
		$select = array();
		$select['cat'] = $ct = JYFilterUtils::unfilterText($this->request->getGetParameter('ct'));
		$select['attr'] = $attr = JYFilterUtils::unfilterText($this->request->getGetParameter('attr'));
		$select['title'] = $tit = JYFilterUtils::unfilterText($this->request->getGetParameter('title'));
		if(isset($_POST['sel_sub'])){
			$select['cat'] = $ct = JYFilterUtils::unfilterText($this->request->getPostParameter('ct'));
			$select['attr'] = $attr = JYFilterUtils::unfilterText($this->request->getPostParameter('attr'));
			$select['title'] = $tit = JYFilterUtils::unfilterText($this->request->getPostParameter('title'));
		}
		
		$titCate = $this->getTitCategory();
		$totalNum = $this->getQuestTotalNum($select);
		$tolpage = $this->totalPage = ceil($totalNum/$pagesize);
		if($page>$tolpage || $page<1){
			$page = 1;
		}
		$allquest = $this->getAllQuestInfo($page,$pagesize,$select);
		$this->totalNum = $totalNum;
		$this->titCate = $titCate;
		$this->allquest = $allquest;
		$this->page = $page;
		$this->pagesize = $pagesize;
		$this->cattype = $ct;
		$this->attr = $attr;
		$this->title = $tit;
		return $this->render();
	}
	public function addAction() {
		if(isset($_POST['tit_add'])){
			$tit['title'] = $this->request->getPostParameter('tit_name');
			if(empty($tit['title'])){
				$this->alert('名称值不能为空，请重新填写');
				exit;
			}
			$tit['cat'] = $this->request->getPostParameter('cate_type');
			$tit['qtype'] = $this->request->getPostParameter('tit_type');
			$tit['allow_null'] = $this->request->getPostParameter('tit_null');
			$tit['note'] = $this->request->getPostParameter('tit_mark');
			$tit['qlevel'] = $this->request->getPostParameter('tit_level');
			$tit['unit_id'] = $this->unid;
			$tit['attr'] = 1;
			$tit['state'] = 1;
			$tit['upd_time'] = date('Y-m-d H:i:s');
			$tit['upd_user'] = $this->username;
			if($row=$this->addQuestTit($tit)){
				$this->alert('添加成功','/index.php?c=quest&a=index');
				exit;
			}
			$this->alert('系统繁忙，请重试！');
			exit;
		}
		$select = JYFilterUtils::unfilterText($this->request->getGetParameter('s'));
		$page = $this->request->getGetParameter('p',1);
		$pagesize = $this->request->getGetParameter('ps',10);
		$titCate = $this->getTitCategory();
		
		$this->titCate = $titCate;
		return $this->render();
	}
	public function editAction() {
//		$ct = JYFilterUtils::unfilterText($this->request->getGetParameter('ct'));
//		$attr = JYFilterUtils::unfilterText($this->request->getGetParameter('attr'));
//		$tit = JYFilterUtils::unfilterText($this->request->getGetParameter('title'));
		if(isset($_POST['tit_edit'])){
			$id = JYFilterUtils::unfilterText($this->request->getPostParameter('id'));
			$tit['title'] = $this->request->getPostParameter('tit_name');
			$tit['cat'] = $this->request->getPostParameter('cate_type');
			$tit['qtype'] = $this->request->getPostParameter('tit_type');
			$tit['allow_null'] = $this->request->getPostParameter('tit_null');
			$tit['note'] = $this->request->getPostParameter('tit_mark');
			$tit['qlevel'] = $this->request->getPostParameter('tit_level');
			$tit['unit_id'] = $this->unid;
			$tit['attr'] = 1;
			$tit['state'] = 1;
			$tit['upd_time'] = date('Y-m-d H:i:s');
			$tit['upd_user'] = $this->username;
			$key = $this->request->getPostParameter('key');
			$verkey = md5($tit['title'].$tit['qtype'].$tit['qlevel'].$tit['allow_null'].$tit['note'].$tit['cat']);
			if($key == $verkey){
				$this->alert('您没有修改任何东西');
				exit;
			}
			if($this->editQuestTit($id,$tit)){
				$this->alert('修改成功');
				exit;
			}
			$this->alert('系统繁忙，请重试');
			exit;
		}
		$id = JYFilterUtils::unfilterText($this->request->getGetParameter('id'));
		$questInfo = $this->getQuestInfoById($id);
		$titCate = $this->getTitCategory();
		$key = md5($questInfo['TITLE'].$questInfo['QTYPE'].$questInfo['QLEVEL'].$questInfo['ALLOW_NULL'].$questInfo['NOTE'].$questInfo['CAT']);
		$this->verkey = $key;
		$this->titCate = $titCate;
		$this->quest = $questInfo;
		$this->id = $id;
		return $this->render();
	}
	function delAction(){
		$id = JYFilterUtils::unfilterText($this->request->getGetParameter('id'));
		if($this->delQuestTit($id)){
			$this->alert('删除成功','/index.php?c=quest&a=index');
			exit;
		}
		$this->alert('系统繁忙，请重试');
		exit;
	}
	function getTitCategory(){ //返回题目分类信息
//		$key = md5(__FUNCTION__.$id.$page.$pagesize.$select);
//		if(JYMemcache::get($key)){
//			return unserialize(JYMemcache::get($key));
//		}
		if($this->unid<0){
			$where = 'state=1 and cat_type=2';
		}else{
			$where = 'state=1 and unit_id='.$this->unid.' and cat_type=2';
		}
		
		$fields = 'cat_id cid,cat_name cn';
		$order = 'upd_time desc';
		$data = JYDB::findAll('CATEGORY',$where,$fields,$order);
//		JYMemcache::set($key,serialize($data),60);
		return $data;
	}
	function getAllQuestInfo($page=0,$pagesize,$select=''){ //返回指定条数指定条件的所有题目信息
//		$key = md5(__FUNCTION__.$id.$page.$pagesize.$select);
//		if(JYMemcache::get($key)){
//			return unserialize(JYMemcache::get($key));
//		}
		if($this->unid<0){
			$where = 'state=1';
		}else{
			$where = 'state=1 and unit_id='.$this->unid;
		}
		
		$fields = '*';
		$order = 'upd_time desc';
		if(count($select)>0){
			foreach ($select as $k=>$v){
				if(!empty($v)){
					if($k=='title'){
						$where .= ' and '.$k.' like \'%'.$v.'%\'';
						continue;
					}
					$where .= ' and '.$k.'='.$v;
				}
			}
		}
		$data = JYDB::findAll($this->tableName,$where,$fields,$order,$page,$pagesize);
//		JYMemcache::set($key,serialize($data),60);
		return $data;
	}
	function getQuestTotalNum($select){ //返回题目的总数
		if(count($select)>0){
			foreach ($select as $k=>$v){
				if(!empty($v)){
					if($k=='title'){
						$where .= ' and '.$k.' like \'%'.$v.'%\'';
						continue;
					}
					$where .= ' and '.$k.'='.$v;
				}
			}
		}
		if($this->unid<0){
			$sql = 'select count(*) num from '.$this->tableName.' where state=1'.$where;
		}else{
			$sql = 'select count(*) num from '.$this->tableName.' where state=1 and unit_id='.$this->unid.$where;
		}
		$num = JYDB::query($sql);
		return $num[0]['NUM'];
	}
	function getQuestInfoById($id){ //得到指定ID的题目信息
		$where = 'q_id='.$id;
		$data = JYDB::find($this->tableName,$where);
		return $data;
	}
	function addQuestTit($arr){ //添加题目
		return JYDB::create($this->tableName,$arr,'Q_ID');
	}
	function editQuestTit($id,$arr){
		return JYDB::update($this->tableName,'q_id='.$id,$arr);
	}
	function delQuestTit($id){
		try {
			return JYDB::update($this->tableName,'q_id='.$id,array('state'=>0));
		}catch (Exception $e){}
	}
}